﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space
{
    public class Bomb : Weapon
    {
        public float Speed { get; set; }
        public float SplashRadius { get; set; }

        public Vector3 TargetPosition { get; set; }

        public override float Angle
        {
            get
            {
                return MathHelper.PiOver2 - (float)Math.Atan2(TargetPosition.Z - Position.Z, TargetPosition.X - Position.X);
            }
        }

        public Bomb(Ship ship)
            : base(ship)
        {
            Alive = true;
        }

        public override void Update(float time)
        {
            Vector3 dest = TargetPosition;
            if ((dest - Position).Length() > Speed)
            {
                Vector3 dir = dest - Position;
                dir.Normalize();
                Position += dir * Speed;
            }
            else
            {
                Alive = false;
                IData data = game.Services.GetService<IData>();

                foreach (Ship s in data.Ships.Where(s => s.Player != Ship.Player && (s.Position - Position).Length() < SplashRadius + s.Radius))
                {
                    s.HP -= Damage;
                }
                foreach (SolarSystem s in data.SolarSystems)
                    foreach (Planet p in s.Planets.Where(p => p.Player != Ship.Player && (p.Position - Position).Length() < SplashRadius + p.Radius))
                    {
                        p.Population -= Damage;
                        if (p.Population <= 0)
                        {
                            p.Player = data.NeutralPlayer;
                            p.Population = 0;
                        }
                    }
                //Target.HP -= Damage;
            }

            base.Update(time);
        }
    }
}
